import re

from django.shortcuts import render
from rest_framework.pagination import PageNumberPagination
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from books.filter import BookFilter
from books.models import User, Type, House, Bookname, Books, Detail, Comment
from books.ser import TypeSer, HouseSer, BooknameSer, BooksSer, DetailSer, CommentSer
from utils.myjwt import pymyjwt

import random

from utils.myredis import redistoll


# Create your views here.
# 发送验证码
class PhoneView(APIView):
    def post(self, request):
        phone=request.data.get('phone')
        if re.match(r'^1[3-9]\d{9}$',phone):
            yan=str(random.randint(1000,9999))
            redistoll.setex(phone,30*2,yan)
            print(f'验证码{yan}已发送到{phone}')
            return Response({'msg':"发送成功",'code':200,'yan':yan})
        else:
            return Response({'msg':"发送失败",'code':400})


# 账号密码登录
class UserView(APIView):
    def post(self, request):
        user=request.data.get('user')
        password=request.data.get('password')
        if user=='' or password=='':
            return Response({'msg':"用户名或密码为空",'code':400})
        info=User.objects.filter(user=user,password=password).first()
        if info is None:
            return Response({'msg':"没有此用户",'code':400})
        else:
            token=pymyjwt.gettoken(info.id,info.user)
            return Response({'msg':"登录成功",'code':200,'token':token,'id':info.id,'user':user})

# 手机号登录
class PhoneLogin(APIView):
    def post(self, request):
        phone=request.data.get('phone')
        yan=request.data.get('yan')
        if re.match(r'^1[3-9]\d{9}$',phone):
            if redistoll.getstr(phone)!=yan:
                return Response({'msg':"验证码错误",'code':400})
            else:
                return Response({'msg':"登录成功",'code':200})
        else:
            return Response({'msg':"手机号错误",'code':400})

# 分页
class PageNum(PageNumberPagination):
    page_size = 8
    max_page_size = 100
    page_query_param = 'page'
    page_size_query_param = 'size'

class TypeView(ModelViewSet):
    queryset = Type.objects.all()
    serializer_class = TypeSer
class HouseView(ModelViewSet):
    queryset = House.objects.all()
    serializer_class = HouseSer
class BooknameView(ModelViewSet):
    queryset = Bookname.objects.all()
    serializer_class = BooknameSer
class BooksView(ModelViewSet):
    queryset = Books.objects.all()
    serializer_class = BooksSer
    pagination_class = PageNum
    filterset_class= BookFilter

class BookView(APIView):
    def get(self, request):
        id = request.query_params.get("id")
        print(id)
        data = Books.objects.filter(id=id).first()
        ser = BooksSer(data)
        if data is None:
            return Response({'msg': '没有此数据', 'code': 400})
        return Response({'msg': 'ok', 'code': 200, 'data': ser.data})


class DetailView(APIView):
    def get(self, request):
        id = request.query_params.get("id")
        print(id)
        data = Detail.objects.filter(id=id).first()
        ser = DetailSer(data)
        if data is None:
            return Response({'msg': '没有此数据', 'code': 400})
        return Response({'msg': 'ok', 'code': 200, 'data': ser.data})

# class DetailView(ModelViewSet):
#     queryset = Detail.objects.all()
#     serializer_class = DetailSer
#     print(DetailSer)

class CommentView(APIView):
    def post(self, request):
        user=request.user
        books=request.data.get('books')
        comment=request.data.get('comment')
        if comment and books:
            com=Comment(comment=comment,books=books,user=user)
            com.save()
            return Response({'msg': '评论成功', 'code': 200})

class CommentListView(APIView):
    def get(self,request):
        booksid=request.query_params.get('booksid')
        data=Comment.objects.filter(books=booksid)
        print(data)
        ser=CommentSer(data,many=True)
        return Response(ser.data)







